package com.libgdxbar.pvzlikegame.objects;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.libgdxbar.pvzlikegame.entities.RectTouchableEntity;

import org.libgdxbar.projects.interfaces.renderer.IShapeRender;

// 演示基础地面类 (地面也是游戏实体，没想到吧)
public class SampleGround extends RectTouchableEntity implements IShapeRender {
	// 行列数量
	int rows = 5, cols = 8;
	// 当前是否显示植物阴影
	boolean hasPlantShadow = true;
	// 植物阴影 (放置提示) 所在行列
	int plantShadowRow = 1, plantShadowCol = 1;

	float blockWidth, blockHeight;

	public SampleGround(int width, int height) {
		super(0, 0, width, height);
		blockWidth = (float) (width - 2) / cols;
		blockHeight = (float) (height - 4) / rows;
	}

	@Override
	public void run(float delta) {
	}

	@Override
	public void render(ShapeRenderer renderer) {
		renderer.setColor(Color.YELLOW);
		for (int i = 0; i <= rows; i++) {
			float height = i * blockHeight + 2;
			renderer.line(0, height, getWidth(), height);
		}
		for (int i = 0; i <= cols; i++) {
			float width = i * blockWidth + 2;
			renderer.line(width, 2, width, getHeight() - 2);
		}
		renderer.setColor(Color.GREEN);
		if (hasPlantShadow) {
			renderer.circle(plantShadowCol * blockWidth - blockWidth / 2, plantShadowRow * blockHeight - blockHeight / 2, 16);
		}
	}

	@Override
	public boolean onTouchUp(int x, int y) {
		return false;
	}

	@Override
	public boolean onTouchDown(int x, int y) {
		plantShadowRow = (int) (y / getHeight() * rows) + 1;
		plantShadowCol = (int) (x / getWidth() * cols) + 1;
		return false;
	}

	@Override
	public boolean onOverlap(int x, int y) {
		plantShadowRow = (int) (y / getHeight() * rows) + 1;
		plantShadowCol = (int) (x / getWidth() * cols) + 1;
		return false;
	}
}
